#!/usr/bin/python
# -*- coding: UTF-8 -*-
__author__ = 'dengqianyi'

import os
import mainHelp
import xlsReloadHelp
from common.xlsComponent import XlsComponent
from common.xlsComponent import IsEmptyCell


_sb_id = "神兵ID".decode('utf-8')
_sb_name = "神兵名称".decode('utf-8')
_sb_desc = "神兵描述".decode('utf-8')
_sb_belong_generate = "所属武将ID".decode('utf-8')
_sb_resource_id = "资源ID".decode('utf-8')
_sb_is_open = "是否开启".decode('utf-8')
_sb_open_by_generate_name = "需拥有武将名字".decode('utf-8')
_sb_open_by_generate_id = "需拥有武将ID".decode('utf-8')
_sb_generate_star_level = "拥有武将星级".decode('utf-8')
_sb_open_equip_name = "需拥有装备名称".decode('utf-8')
_sb_open_equip_id = "拥有装备ID".decode('utf-8')
_sb_open_level = "主公等级".decode('utf-8')
_sb_is_version = "属于当前版本".decode('utf-8')
_sb_peizhi = "神兵配置".decode('utf-8')
_sb_zongdejinengbiao = "总的技能表".decode('utf-8')
_sb_sgs_skill_effect = "sgs_skill_effect".decode('utf-8')


_sb_wujiang = "神兵武将".decode('utf-8')
_sb_xdlx = "星点类型".decode('utf-8')
_sb_ssys = "所属页数".decode('utf-8')
_sb_xdsx = "星点顺序".decode('utf-8')
_sb_xq1lx = "需求1类型".decode('utf-8')
_sb_xq1mb = "需求1目标".decode('utf-8')
_sb_xq1sl = "需求1数量".decode('utf-8')
_sb_xq2lx = "需求2类型".decode('utf-8')
_sb_xq2mb = "需求2目标".decode('utf-8')
_sb_xq2sl = "需求2数量".decode('utf-8')
_sb_xq3lx = "需求3类型".decode('utf-8')
_sb_xq3mb = "需求3目标".decode('utf-8')
_sb_xq3sl = "需求3数量".decode('utf-8')
_sb_xdxg = "星点效果".decode('utf-8')
_sb_xdsz = "星点数值".decode('utf-8')
_sb_xdid = "星点id".decode('utf-8')
_sb_is_vip = "是否VIP专有".decode('utf-8')
_sb_vip_level = "VIP等级".decode('utf-8')
_sb_jiachengpeizhi = "神兵加成配置设计".decode('utf-8')

# _sb_skill_title=["skill_id","level","skill_name","skill_code","short_desc","phase_desc","range_desc","skill_desc","desc_param_value","desc_param_step","desc_param_num","resource_id","skill_grade","skill_type	skill_effect","skill_effect_prob","skill_alias","first_min_round","first_max_round","skill_min_round","skill_max_round","skill_round_step","skill_fighting","skill_fighting_step","is_absorb","is_open","skill_piece_per","compose_need_piece_num","warlord_general_id","upgrade_exp_type","upgrade_coin_type","eated_exp_type","can_fit","major_property","is_version"]

class Shenbing(object):
    def __init__(self,xlsDesignPathName,xlsCodePathName):
        self._xlsDesignPathName = xlsDesignPathName
        self._xlsCodePathName = xlsCodePathName
        self.init()
        pass
    def init(self):
        self._xlsDesignComponent = XlsComponent(self._xlsDesignPathName)
        self._xlsCodeComponent = XlsComponent(self._xlsCodePathName,isOutFileFlag=True)

        #关注相应的消息
        mainHelp.Connection.addEvents("onReceiveMessage",self.onReceiveMessage)
        #把相应的xls添加到监控
        xlsReloadHelp.getInstance().addXlsToMonitor(self._xlsDesignPathName)
        pass
    def msgHandler(self,jsonObj):
        if jsonObj.get("msgFunc") == "shenbing_sgs_magic":
            self.shenbing_sgs_magic(jsonObj)
            return
        if jsonObj.get("msgFunc") == "shenbing_sgs_magic_open_conf":
            self.shenbing_sgs_magic_open_conf(jsonObj)
            return
        if jsonObj.get("msgFunc") == "shenbing_sgs_magic_buff_conf":
            self.shenbing_sgs_magic_buff_conf(jsonObj)
            return
        if jsonObj.get("msgFunc") == "shenbing_sgs_skill_advanced_conf":
            self.shenbing_sgs_skill_advanced_conf(jsonObj)
            return
        if jsonObj.get("msgFunc") == "shenbing_sgs_xml":
            self.sgs_shenbing_xml(jsonObj)
            return
        pass
    def onReceiveMessage(self,jsonObj):
        if jsonObj.get("msgType") != "Shenbing":
            return
        print "Shenbing onReceiveMessage",jsonObj
        self.msgHandler(jsonObj)
        pass
    def shenbing_sgs_skill_advanced_conf_ex(self,rowContentDict):
        def __tmpFunction(skillID,level,contentList,targetDict):
            _newID = str(skillID)+str(int(level)+1)
            targetDict["skill_effect"] = None
            for _index in xrange(0,len(contentList)):
                _rowContent = contentList[_index]
                _tmpID = str(_rowContent.get("effect_id"))[:-1]
                if long(_tmpID) == long(_newID):
                    if targetDict.get("skill_effect") == None:
                        targetDict["skill_effect"] = str(_rowContent.get("effect_id"))+','
                    else:
                        targetDict["skill_effect"] = targetDict.get("skill_effect")+str(_rowContent.get("effect_id"))+','
            pass
        _designConfigContentList = self._xlsDesignComponent.getXlsContentDict().get(_sb_sgs_skill_effect)

        __tmpFunction(rowContentDict.get("skill_id"),rowContentDict.get("level"),_designConfigContentList,rowContentDict)
        pass
    def shenbing_sgs_skill_advanced_conf(self,jsonObj):
        print "shenbing_sgs_skill_advanced_conf begin"
        _designConfigContentList = self._xlsDesignComponent.getXlsContentDict().get(_sb_zongdejinengbiao)
        _designSheetSpecialKeyDict = self._xlsDesignComponent.getSheetSpecialKeyDict(_sb_zongdejinengbiao)
        _designXlsSpecialKeyDict = self._xlsDesignComponent.getXlsSpecialKeyDict()
        # 做key value替换
        self._xlsDesignComponent.replaceKeyByValue(_designConfigContentList,_designSheetSpecialKeyDict,_designXlsSpecialKeyDict)

        _codeConfigContentList = self._xlsCodeComponent.getXlsContentDict()["sgs_skill_advanced_conf"]

        # 抽取level有值的行做处理
        for _index in xrange(0,len(_designConfigContentList)):
            _contentDict = _designConfigContentList[_index]
            # print "shenbing_sgs_skill_advanced_conf _contentDict",_contentDict.get("level")
            if _contentDict.get("level") != None and str(_contentDict.get("level")) != "0":
                _tmpDict = _contentDict
                for key,value in _tmpDict.items():
                    _tmpDict["skill_name"] = str(_tmpDict.get("skill_id"))+str(_tmpDict.get("level"))+"01"
                    _tmpDict["short_desc"] = str(_tmpDict.get("skill_id"))+str(_tmpDict.get("level"))+"02"
                    _tmpDict["phase_desc"] = str(_tmpDict.get("skill_id"))+str(_tmpDict.get("level"))+"03"
                    _tmpDict["range_desc"] = str(_tmpDict.get("skill_id"))+str(_tmpDict.get("level"))+"04"
                    _tmpDict["skill_desc"] = str(_tmpDict.get("skill_id"))+str(_tmpDict.get("level"))+"05"
                # self.shenbing_sgs_skill_advanced_conf_ex(_tmpDict)
                _codeConfigContentList.append(_tmpDict)
        self._xlsCodeComponent.saveLastContentToXls()
        pass
    def sgs_shenbing_xml(self,jsonObj):
        _sgsMagicContentDict = self._xlsCodeComponent.getXlsContentDict().get("sgs_magic")
        _shengchengyuanfenXMLPath = os.path.join(os.path.split(self._xlsCodeComponent.getXlsPathDir())[0],'sgs_magic.xml')
        self._xlsCodeComponent.saveContentToXmlFile(_sgsMagicContentDict,_shengchengyuanfenXMLPath,"sgs_magic")

        _sgsMagicContentDict = self._xlsCodeComponent.getXlsContentDict().get("sgs_magic_open_conf")
        _shengchengyuanfenXMLPath = os.path.join(os.path.split(self._xlsCodeComponent.getXlsPathDir())[0],'sgs_magic_open_conf.xml')
        self._xlsCodeComponent.saveContentToXmlFile(_sgsMagicContentDict,_shengchengyuanfenXMLPath,"sgs_magic_open_conf")

        _sgsMagicContentDict = self._xlsCodeComponent.getXlsContentDict().get("sgs_skill_advanced_conf")
        _shengchengyuanfenXMLPath = os.path.join(os.path.split(self._xlsCodeComponent.getXlsPathDir())[0],'sgs_skill_advanced_conf.xml')
        self._xlsCodeComponent.saveContentToXmlFile(_sgsMagicContentDict,_shengchengyuanfenXMLPath,"sgs_skill_advanced_conf")

        _sgsMagicContentDict = self._xlsCodeComponent.getXlsContentDict().get("sgs_magic_buff_conf")
        _shengchengyuanfenXMLPath = os.path.join(os.path.split(self._xlsCodeComponent.getXlsPathDir())[0],'sgs_magic_buff_conf.xml')
        self._xlsCodeComponent.saveContentToXmlFile(_sgsMagicContentDict,_shengchengyuanfenXMLPath,"sgs_magic_buff_conf")
        return
    def shenbing_sgs_magic_open_conf(self,jsonObj):
        print "shenbing_sgs_magic_open_conf begin"
        _designConfigContentList = self._xlsDesignComponent.getXlsContentDict().get(_sb_peizhi)
        _designSheetSpecialKeyDict = self._xlsDesignComponent.getSheetSpecialKeyDict(_sb_peizhi)
        _designXlsSpecialKeyDict = self._xlsDesignComponent.getXlsSpecialKeyDict()
        _codeConfigContentList = self._xlsCodeComponent.getXlsContentDict()["sgs_magic_open_conf"]
        #做keyvalue替换
        self._xlsDesignComponent.replaceKeyByValue(_designConfigContentList,_designSheetSpecialKeyDict,_designXlsSpecialKeyDict)
        #清空
        del _codeConfigContentList[:]
        for _index in xrange(0,len(_designConfigContentList)):
            _rawContentDict = _designConfigContentList[_index]
            for key,value in _rawContentDict.items():
                if key == _sb_open_by_generate_name and IsEmptyCell(value) == False:
                    _tmpDict = dict()
                    _tmpDict["magic_id"] = _rawContentDict.get(_sb_id)
                    _tmpDict["type"] = 1
                    _tmpDict['conf_id'] = None
                    _tmpDict["object_id"] = _rawContentDict.get(_sb_open_by_generate_id)
                    _tmpDict["condition"] = _rawContentDict.get(_sb_generate_star_level)
                    _tmpDict["condition_desc"] = str(_rawContentDict.get(_sb_open_by_generate_name))+'达到'.decode('utf-8')+str(_tmpDict["condition"])+'星'.decode('utf-8')
                    _codeConfigContentList.append(_tmpDict)
                if key == _sb_generate_star_level and IsEmptyCell(value) == False:
                    _tmpDict = dict()
                    _tmpDict["magic_id"] = _rawContentDict.get(_sb_id)
                    _tmpDict["type"] = 2
                    _tmpDict["object_id"] = _rawContentDict.get(_sb_open_equip_id)
                    _tmpDict["condition"] = 1
                    _tmpDict['conf_id'] = None
                    _tmpDict["condition_desc"] = str(_rawContentDict.get(_sb_open_equip_name))+'图鉴点亮'.decode('utf-8')
                    _codeConfigContentList.append(_tmpDict)
                if key == _sb_open_level and IsEmptyCell(value) == False:
                    _tmpDict = dict()
                    _tmpDict["magic_id"] = _rawContentDict.get(_sb_id)
                    _tmpDict["type"] = 3
                    _tmpDict["object_id"] = 0
                    _tmpDict['conf_id'] = None
                    _tmpDict["condition"] = _rawContentDict.get(_sb_open_level)
                    _tmpDict["condition_desc"] = "主公等级达到".decode('utf-8')+str(_rawContentDict.get(_sb_open_level))+'级'.decode('utf-8')
                    _codeConfigContentList.append(_tmpDict)
        self._xlsCodeComponent.saveLastContentToXls()
        pass
    def _queryI8nId(self,stringId):
        # 查询stringId是否已存在
        _codeI18nContentList = self._xlsCodeComponent.getXlsContentDict()["sgs_i8n"]
        for _index in xrange(0,len(_codeI18nContentList)):
            _contentDict = _codeI18nContentList[_index]
            if _contentDict.get(stringId)!= None:
                return True
        return False
        pass
    def shenbing_sgs_magic(self,jsonObj):
        print "shenbing_sgs_magic begin"
        _designConfigContentList = self._xlsDesignComponent.getXlsContentDict().get(_sb_peizhi)
        _designSheetSpecialKeyDict = self._xlsDesignComponent.getSheetSpecialKeyDict(_sb_peizhi)
        _designXlsSpecialKeyDict = self._xlsDesignComponent.getXlsSpecialKeyDict()
        _codeConfigContentList = self._xlsCodeComponent.getXlsContentDict()["sgs_magic"]
        # 往sgs_i18n表里写
        _codeI18nContentList = self._xlsCodeComponent.getXlsContentDict()["sgs_i8n"]
        #做keyvalue替换
        self._xlsDesignComponent.replaceKeyByValue(_designConfigContentList,_designSheetSpecialKeyDict,_designXlsSpecialKeyDict)
        # print "_designConfigContentList",_designConfigContentList
        # print "_designSheetSpecialKeyDict",_designSheetSpecialKeyDict
        # print "_designXlsSpecialKeyDict",_designXlsSpecialKeyDict
        #清空
        del _codeConfigContentList[:]
        del _codeI18nContentList[:]
        # print "shenbing_sgs_magic",self._xlsDesignComponent.getXlsContentDict().get(_sb_peizhi),self._xlsDesignComponent.getTitleDict().get(_sb_peizhi)
        for _index in xrange(0,len(_designConfigContentList)):
            _rawContentDict = _designConfigContentList[_index]
            _tmpDict = dict()
            for key,value in _rawContentDict.items():
                if key == _sb_id:
                    _tmpDict["magic_id"] = value
                if key == _sb_name:
                    _tmpDict["magic_name"] = value
                if key == _sb_desc:
                    _tmpDict["magic_desc"] = value
                if key == _sb_belong_generate:
                    _tmpDict["general_id"] = value
                if key == _sb_resource_id:
                    _tmpDict["resource_id"] = value
                if key == _sb_is_open:
                    _tmpDict["is_open"] = value
                if key == _sb_is_version:
                    _tmpDict["is_version"] = value
            #i18d生成
            _name_id = "shenbing_name_"+_tmpDict.get("magic_id")
            if self._queryI8nId(_name_id) == False:
                _tmpI18nDict = dict()
                _tmpI18nDict["i18n_jp"]=_tmpI18nDict["i18n_us"]=_tmpI18nDict["i18n_tw"]=_tmpI18nDict["i18n_id"]=_tmpI18nDict["i18n_cn"]=0
                _tmpI18nDict["i18n_id"] = _name_id
                _tmpI18nDict["i18n_cn"] = _tmpDict.get("magic_name")
                _codeI18nContentList.append(_tmpI18nDict)
            _desc_id = "shenbing_desc_"+_tmpDict.get("magic_id")
            if self._queryI8nId(_desc_id) == False:
                _tmpI18nDict = dict()
                _tmpI18nDict["i18n_jp"]=_tmpI18nDict["i18n_us"]=_tmpI18nDict["i18n_tw"]=_tmpI18nDict["i18n_id"]=_tmpI18nDict["i18n_cn"]=0
                _tmpI18nDict["i18n_id"] = _desc_id
                _tmpI18nDict["i18n_cn"] = _tmpDict.get("magic_desc")
                _codeI18nContentList.append(_tmpI18nDict)
            _tmpDict["magic_name"] = _name_id #所有的中文都变成中文
            _tmpDict["magic_desc"] = _desc_id
            _codeConfigContentList.append(_tmpDict)
        self._xlsCodeComponent.saveLastContentToXls()
        pass
    def shenbing_sgs_magic_buff_conf(self,jsonObj):
        print "shenbing_sgs_magic_buff_conf begin"
        _designConfigContentList = self._xlsDesignComponent.getXlsContentDict().get(_sb_jiachengpeizhi)
        _designSheetSpecialKeyDict = self._xlsDesignComponent.getSheetSpecialKeyDict(_sb_jiachengpeizhi)
        _designXlsSpecialKeyDict = self._xlsDesignComponent.getXlsSpecialKeyDict()
        _codeConfigContentList = self._xlsCodeComponent.getXlsContentDict()["sgs_magic_buff_conf"]
        #做keyvalue替换
        self._xlsDesignComponent.replaceKeyByValue(_designConfigContentList,_designSheetSpecialKeyDict,_designXlsSpecialKeyDict)
        # print "_designConfigContentList",_designConfigContentList
        # print "_designSheetSpecialKeyDict",_designSheetSpecialKeyDict
        # print "_designXlsSpecialKeyDict",_designXlsSpecialKeyDict
        #清空
        del _codeConfigContentList[:]
        # print "shenbing_sgs_magic",self._xlsDesignComponent.getXlsContentDict().get(_sb_peizhi),self._xlsDesignComponent.getTitleDict().get(_sb_peizhi)
        for _index in xrange(0,len(_designConfigContentList)):
            _rawContentDict = _designConfigContentList[_index]
            _tmpDict = dict()
            self.sgs_magic_buff_conf_exA(_sb_xq1lx,_rawContentDict,_codeConfigContentList,_tmpDict)
            self.sgs_magic_buff_conf_exA(_sb_xq2lx,_rawContentDict,_codeConfigContentList,_tmpDict)
            self.sgs_magic_buff_conf_exA(_sb_xq3lx,_rawContentDict,_codeConfigContentList,_tmpDict)
            _codeConfigContentList.append(_tmpDict)
        self._xlsCodeComponent.saveLastContentToXls()
        return
    def sgs_magic_buff_conf_exA(self,xq_type,rawContentDict,targetContentList,outputDict):
        # 3个不同的需求
        if IsEmptyCell(rawContentDict.get(xq_type)) == False:
            # _tmpDict = dict()
            outputDict["magic_id"] = rawContentDict.get(_sb_id)
            outputDict["level"] = rawContentDict.get(_sb_ssys)
            outputDict["sub_level"] = rawContentDict.get(_sb_xdsx)
            outputDict["conf_id"] = None
            if rawContentDict.get(_sb_xdlx) == "普通".decode('utf-8') or \
                rawContentDict.get(_sb_xdlx) == 0:
                outputDict["is_vip"] = 0
                outputDict["user_vip_level"] = 0
            else:
                #所有的高级 星点类型，其VIP等级都要求10
                outputDict["is_vip"] = 1
                outputDict["user_vip_level"] = 10
            outputDict["buff_type"] = rawContentDict.get(_sb_xdxg)
            outputDict["buff_effect"] = rawContentDict.get(_sb_xdsz)
            outputDict["buff_desc"] = 0
            if xq_type == _sb_xq1lx and IsEmptyCell(rawContentDict.get(_sb_xq1lx)) == False:
                outputDict["forge_piece_id"] = rawContentDict.get(_sb_xq1mb)
                outputDict["forge_piece_num"] = rawContentDict.get(_sb_xq1sl)
                outputDict["forge_piece_type"] = rawContentDict.get(_sb_xq1lx)
            elif xq_type == _sb_xq2lx and IsEmptyCell(rawContentDict.get(_sb_xq2lx)) == False:
                # print "outputDict.get('forge_piece_id')",outputDict.get("forge_piece_id")
                if outputDict.get("forge_piece_id") != None and len(str(outputDict.get("forge_piece_id"))) > 1:#一般来说，ID的长度都会大于1
                    outputDict["forge_piece_id"] = str(outputDict.get("forge_piece_id"))+';'.decode('utf-8')+str(rawContentDict.get(_sb_xq2mb))
                    outputDict["forge_piece_num"] =str(outputDict.get("forge_piece_num"))+';'.decode('utf-8')+str(rawContentDict.get(_sb_xq2sl))
                    outputDict["forge_piece_type"] = str(outputDict.get('forge_piece_type'))+';'.decode('utf-8')+str(rawContentDict.get(_sb_xq2lx))
                else:
                    outputDict["forge_piece_id"] = rawContentDict.get(_sb_xq2mb)
                    outputDict["forge_piece_num"] = rawContentDict.get(_sb_xq2sl)
                    outputDict["forge_piece_type"] = rawContentDict.get(_sb_xq2lx)
            elif xq_type == _sb_xq3lx and IsEmptyCell(rawContentDict.get(_sb_xq3lx)) == False:
                if outputDict.get("forge_piece_id")!= None and len(str(outputDict.get("forge_piece_id"))) > 1:#一般来说，ID的长度都会大于1
                    outputDict["forge_piece_id"] = str(outputDict.get("forge_piece_id"))+';'.decode('utf-8')+str(rawContentDict.get(_sb_xq3mb))
                    outputDict["forge_piece_num"] =str(outputDict.get("forge_piece_num"))+';'.decode('utf-8')+str(rawContentDict.get(_sb_xq3sl))
                    outputDict["forge_piece_type"] = str(outputDict.get('forge_piece_type'))+';'.decode('utf-8')+str(rawContentDict.get(_sb_xq3lx))
                else:
                    outputDict["forge_piece_id"] = rawContentDict.get(_sb_xq3mb)
                    outputDict["forge_piece_num"] = rawContentDict.get(_sb_xq3sl)
                    outputDict["forge_piece_type"] = rawContentDict.get(_sb_xq3lx)
    pass